package com.stoken.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stoken.dao.MemberBaseInfoDao;
import com.stoken.dao.MemberShopWalletDao;
import com.stoken.dao.MemberTowalletListDao;
import com.stoken.dao.MemberTransinListDao;
import com.stoken.dao.MemberWalletDao;
import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberExtractList;
import com.stoken.domain.MemberShopWallet;
import com.stoken.domain.MemberTowalletList;
import com.stoken.domain.MemberTransinList;
import com.stoken.domain.MemberTransoutList;
import com.stoken.domain.MemberWallet;
import com.stoken.service.MemberExtractListService;
import com.stoken.service.MemberTowalletListService;
import com.stoken.service.MemberTransinListService;
import com.stoken.service.MemberTransoutListService;
import com.stoken.service.MemberWalletService;
import com.symbio.dao.hibernate.GenericDaoImpl;
import com.symbio.service.impl.AbstractServiceImpl;

@Service
public class MemberWalletServiceImpl extends AbstractServiceImpl<MemberWallet,Long> implements MemberWalletService {
    
	@Autowired
	private MemberWalletDao memberWalletDao;
	
	/**
	 * 会员基本信息实体操作
	 */
	@Autowired
	private MemberBaseInfoDao memberInfoDao;
	
	@Autowired
	private MemberExtractListService memberExtractListService;
	/**
	 * 会员转入记录表实体操作
	 */
	@Autowired
	private MemberTransinListService meTransinLiService;
	
	@Autowired
	private MemberTransoutListService meTransoutLiService;
	
	@Autowired
	private MemberTowalletListService meTowalletListService;
	
	@Autowired
	private MemberTowalletListDao memberTowalletListDao;
	
	@Autowired
	private MemberShopWalletDao memberShopWalletDao;
	
	@Override
	public GenericDaoImpl<MemberWallet, Long> getDao() {		
		return memberWalletDao;
	}

	@Override
	public String verificationBalance(Double balance,String userAccount) {
		//查询改账户余额
		MemberWallet mbWallet = memberWalletDao.find("from MemberWallet where baseUsername.baseUsername = ?", new Object[]{userAccount});		
		return balance-mbWallet.getWalletRemain() > 0? "1":"0";
	} 
    
	
	@Override
	public void withdrawals(Double balance, String userAccount) {		
		
		//修改会员余额
		MemberWallet mbWallet = memberWalletDao.find("from MemberWallet where baseUsername.baseUsername = ?", new Object[]{userAccount});	
		Double withdrawlsBalance = mbWallet.getWalletRemain()-balance;
		mbWallet.setWalletRemain(withdrawlsBalance);
		memberWalletDao.update(mbWallet);
		
		//插入提现记录
		MemberExtractList mExList = new MemberExtractList();
		MemberBaseInfo memberBaseInfo = memberInfoDao.find("baseUsername",userAccount);
		mExList.setBaseUsername(memberBaseInfo);
		mExList.setBaseName(memberBaseInfo.getBaseName());
		mExList.setExtractApplyTime(new Date());
		mExList.setExtractAmount(balance);
		mExList.setExtractHaddingfee(balance*StokenRule.WITHDRAWALSFEE);  //提现手续费
		mExList.setExtractAmountFact(balance-mExList.getExtractHaddingfee());
		mExList.setExtractSubbranch(memberBaseInfo.getBaseSubbranch());
		mExList.setExtractAccount(memberBaseInfo.getBaseAccount());
		mExList.setExtractState(StokenRule.EXTRACTSTATE_UNTREATED);
		mExList.setWalletRemain(withdrawlsBalance);
		memberExtractListService.save(mExList);
		
	}

	@Override
	public void transferOfCash(Double money, String userAccount,
			String targetAcount) {
		//修改用户余额  目标账户		
		MemberWallet mbWallet = memberWalletDao.find("from MemberWallet where baseUsername.baseUsername = ?", new Object[]{targetAcount});	
		Double withdrawlsBalance = mbWallet.getWalletRemain()+money;
		mbWallet.setWalletRemain(withdrawlsBalance);
		memberWalletDao.update(mbWallet);
		
		//修改账户余额  转出账户
		MemberWallet mbWalletOut = memberWalletDao.find("from MemberWallet where baseUsername.baseUsername = ?", new Object[]{userAccount});	
		Double balanceOut = mbWalletOut.getWalletRemain()-money;
		mbWalletOut.setWalletRemain(balanceOut);
		memberWalletDao.update(mbWalletOut);
		
		//转入用户记录
		MemberTransinList memTranList = new MemberTransinList();
		MemberBaseInfo memberBaseInfo = memberInfoDao.find("baseUsername",userAccount);
		MemberBaseInfo tMemberBaseInfo = memberInfoDao.find("baseUsername",targetAcount);
		memTranList.setBaseUsername(tMemberBaseInfo);
		memTranList.setTransinAmount(money);		
		memTranList.setTransUsername(memberBaseInfo);
		memTranList.setTransTime(new Date());
		memTranList.setWalletRemain(withdrawlsBalance);
		meTransinLiService.save(memTranList);
		
		//转出用户记录
		MemberTransoutList memTranoutList = new MemberTransoutList();
		memTranoutList.setBaseUsername(memberBaseInfo);
		memTranoutList.setTransinUsername(tMemberBaseInfo);
		memTranoutList.setTransoutAmount(money);		
		memTranoutList.setTransoutTime(new Date());
		memTranoutList.setWalletRemain(balanceOut);
		meTransoutLiService.save(memTranoutList);
		
	}

	@Override
	public void transferOfConsume(Double money, String userAccount) {
		//修改账户余额
		MemberWallet mbWallet = memberWalletDao.find("from MemberWallet where baseUsername.baseUsername = ?", new Object[]{userAccount});	
		Double withdrawlsBalance = mbWallet.getWalletRemain()-money;
		mbWallet.setWalletRemain(withdrawlsBalance);
		memberWalletDao.update(mbWallet);
		
		//修改消费基金账户
		MemberShopWallet mShWallet = memberShopWalletDao.find("from MemberShopWallet where baseUsername.baseUsername = ?", new Object[]{userAccount});
		mShWallet.setShopWalletBalance(mShWallet.getShopWalletBalance() + money);
		mShWallet.setShopWalletLastTime(new Date());
		memberShopWalletDao.update(mShWallet);
		
		
		//插入转账消费记录
		MemberTowalletList meTowalletList = new MemberTowalletList();
		MemberBaseInfo memberBaseInfo = memberInfoDao.find("from MemberBaseInfo where baseUsername = ?", new Object[]{userAccount});
		meTowalletList.setBaseUsername(memberBaseInfo);
		meTowalletList.setTowalletAmount(money);
		meTowalletList.setTowalletTime(new Date());
		meTowalletList.setWalletRemain(withdrawlsBalance);		
		memberTowalletListDao.save(meTowalletList);
		
		
		
	}

	@Override
	public MemberWallet findWalletByUsername(String userAccount) {
		return memberWalletDao.find("from MemberWallet where baseUsername.baseUsername = ?", new Object[]{userAccount});
		//return memberWalletDao.find("walletRemark",userAccount);
	}

}
